﻿using System;
using System.Windows.Input;
using System.Windows;
using SurveillancePro.Base.Common;
using System.Windows.Media;
using System.Diagnostics;
using Microsoft.Practices.Prism.Regions;
using InteractiveWhiteboardManagement.Base.Common;

namespace SurveillancePro.Base.ViewModels
{
    /// <summary>
    /// This ViewModelBase subclass requests to be removed 
    /// from the UI when its CloseCommand executes.
    /// This class is abstract.
    /// </summary>
    public abstract class WorkspaceViewModel : ViewModelBase
    {
        #region Fields

        private RelayCommand _closeCommand;

        #endregion // Fields

        #region Constructor

        protected WorkspaceViewModel()
        {
        }

        #endregion // Constructor

        #region Property

        /// <summary>
        /// RegionManager
        /// </summary>
        public IRegionManager BaseRegionManager { get; protected set; }

        private string _headerImageSource;
        /// <summary>
        /// 图片资源
        /// </summary>
        public virtual string HeaderImageSource
        {
            get
            {
                return _headerImageSource;
            }
            protected set
            {
                if (_headerImageSource == value) return;
                _headerImageSource = value;
                base.OnPropertyChanged("HeaderImageSource");
            }
        }

        private Visibility _isShowCloseButton;
        /// <summary>
        /// 是否显示关闭按钮
        /// </summary>
        public virtual Visibility IsShowCloseButton
        {
            get
            {
                return _isShowCloseButton;
            }
            protected set
            {
                if (_isShowCloseButton == value) return;
                _isShowCloseButton = value;
                base.OnPropertyChanged("IsShowCloseButton");
            }
        }

        private string _viewName;
        /// <summary>
        /// 对应视图名称
        /// </summary>
        public virtual string ViewName
        {
            get
            {
                return _viewName;
            }
            protected set
            {
                if (_viewName == value) return;
                _viewName = value;
                base.OnPropertyChanged("ViewName");
            }
        }

        #endregion

        #region CloseCommand

        /// <summary>
        /// Returns the command that, when invoked, attempts
        /// to remove this workspace from the user interface.
        /// </summary>
        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                    _closeCommand = new RelayCommand(param => this._onRequestClose());

                return _closeCommand;
            }
        }

        #endregion // CloseCommand

        #region RequestClose [event]

        /// <summary>
        /// Raised when this workspace should be removed from the UI.
        /// </summary>
        public event EventHandler RequestClose;

        private void _onRequestClose()
        {
            try
            {
                _onWorkspaceRequestClose(); // 关闭 Tab

                EventHandler handler = this.RequestClose;
                if (handler != null)
                    handler(this, EventArgs.Empty);
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        /// <summary>
        /// 关闭 Tab
        /// </summary>
        private void _onWorkspaceRequestClose()
        {
            if (this.BaseRegionManager == null ||
                string.IsNullOrWhiteSpace(ViewName))
                return;

            IViewsCollection regionViews = this.BaseRegionManager.Regions[RegionNames.ContentRegion].Views;
            foreach (object view in regionViews)
            {
                if (view is FrameworkElement &&
                    (view as FrameworkElement).DataContext is WorkspaceViewModel &&
                    !string.IsNullOrWhiteSpace(((view as FrameworkElement).DataContext as WorkspaceViewModel).HeaderTitle) &&
                    !string.IsNullOrWhiteSpace(base.HeaderTitle) &&
                    ((view as FrameworkElement).DataContext as WorkspaceViewModel).HeaderTitle.Equals(base.HeaderTitle) &&
                    ViewName.Equals(view.GetType().Name)) // 循环判断，若视图名称相等则移除
                {
                    this.OnDispose(); // 释放资源
                    this.BaseRegionManager.Regions[RegionNames.ContentRegion].Remove(view);
                    break;
                }
            }
        }

        #endregion // RequestClose [event]

        #region Error Handling

        /// <summary>
        /// 
        /// </summary>
        /// <param name="errorMsg">content</param>
        /// <param name="title">caption</param>
        public void ShowError(string errorMsg, string title)
        {
            MessageBox.Show(errorMsg, title, MessageBoxButton.OK);
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// 关闭所有 Tab 页
        /// </summary>
        protected void CloseWorkspaceTabs()
        {
            if (this.BaseRegionManager == null)
                return;

            IViewsCollection regionViews = this.BaseRegionManager.Regions[RegionNames.ContentRegion].Views;
            foreach (object view in regionViews) // 移除所有视图
            {
                ((view as FrameworkElement).DataContext as WorkspaceViewModel).OnDispose(); // 释放资源
                this.BaseRegionManager.Regions[RegionNames.ContentRegion].Remove(view);
            }
        }

        #endregion
    }
}